const path = require('path');
const glob = require('glob');
const fs = require('fs');
const { writeYml } = require('./store');
const Parser = require('./parser');
const Comment = require('./comment');

const { config } = require('../swagger.config');
const routes = glob.globSync(config.routers, {dotRelative: true});
for(const route of routes) {
  const parser = new Parser(fs.readFileSync(route).toString());
  const commentsOpts = parser.comments;
  const baseName = path.basename(route);
  let index = 0;
  for(const commentsOpt of commentsOpts) {
    index++;
    const comment = new Comment(commentsOpt);
    if (!comment.isEmpty) {
      const yml = comment.toYml();
      writeYml(path.join(config.docsRoot, baseName.replace('.js', '.yml')), yml, index !== 1);
    }
  }  
}
